package com.frame.platform.dept;

import java.util.List;
import com.frame.pageform.annotation.Login;
import org.apache.commons.lang.StringUtils;

import com.frame.common.SystemConfig;
import com.frame.pageform.*;
import com.frame.pageform.component.JsonObject;
import com.frame.session.UserSession;

@Login
public class DeptPage extends PageForm{

    private Wfdept bean;
    private String deptId;
    private String parDeptId;
    private String actions;
    private String state;
    private List subDeptList;
    private String[] deptIds;
    static{
        initForward("/platform/dept/dept.page");
    }
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if("addShow".equals(getActions())){
            setParDeptId(getDeptId());
            setActions("add");
            return forward("add");
        }else if("add".equals(getActions())){
            DeptDao dao=new DeptDao();
            JsonObject jsonobj=new JsonObject();
            if(dao.checkDeptName(getBean().getDeptname(),getBean().getDeptid(),new Integer(getParDeptId()))){
                if("root".equals(getParDeptId()))
                    setParDeptId("1");
                getBean().setDeptid(null);
                getBean().setPardeptid(new Integer(getParDeptId()));
                dao.addDept(getBean());
                jsonobj.getJsonPage().closeWin("{\"id\":\""+bean.getDeptid()+"\",\"text\":\""+bean.getDeptname()+"\",\"iconUrl\":\""+SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png\"}");
            }
            else
                jsonobj.getJsonPage().showAlertMsg("机构名称重复了");
            return jsonobj.toString();
        }
        else if("updateShow".equals(getActions())){
            bean=new DeptDao().getDept(new Integer(getDeptId()));
            setParDeptId(bean.getPardeptid().toString());
            setActions("update");
            return forward("add");
        }
        else if("update".equals(getActions())){
            DeptDao dao=new DeptDao();
            JsonObject jsonobj=new JsonObject();
            if(dao.checkDeptName(getBean().getDeptname(),getBean().getDeptid(),new Integer(getParDeptId()))){
                dao.updateDept(getBean());
                jsonobj.getJsonPage().closeWin(bean.getDeptname());
            }
            else
                jsonobj.getJsonPage().showAlertMsg("机构名称重复了");
            return jsonobj.toString();
        }
        else if("del".equals(getActions())){
            new DeptDao().delDept(new Integer(getDeptId()));
            JsonObject jsonobj=new JsonObject();
            jsonobj.put("state", "ok");
            return jsonobj.toString();
        }
        else if("sortShow".equals(getActions())){
            if("root".equals(getParDeptId()))
                setParDeptId("1");
            subDeptList=new DeptDao().getSubDept(getParDeptId());
            setActions("sort");
            return forward("sort");
        }
        else if("sort".equals(getActions())){
            if(getDeptIds()!=null)
                new DeptDao().sortDept(getDeptIds());
            JsonObject jsonObj=new JsonObject();
            jsonObj.put("state", "ok");
            return jsonObj.toString();
        }
        return forward();
    }

    public String initDeptTree() throws Exception{
        DeptDao deptDao=new DeptDao();
        String json="[]";
        JsonObject jsonobj=getJsonObject();
        if(UserSession.getAdminUser()!=null){
            Wfdept gradedept=null;
            if(StringUtils.isNotBlank(deptId))
                gradedept=deptDao.getDept(new Integer(deptId));
            else
                gradedept=deptDao.getDept(1);
            List<Wfdept> dlist=deptDao.getSubDept(gradedept.getDeptid().toString());
            StringBuffer s=new StringBuffer();
            s.append("[{");
            s.append(jsonobj.quote("id")+":"+jsonobj.quote(gradedept.getId())+","+jsonobj.quote("text")+":"+jsonobj.quote(gradedept.getName())+","+jsonobj.quote("iconUrl")+":"+jsonobj.quote(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png"));
            s.append(","+jsonobj.quote("children")+":"+"[");
            for(int i=0;i<dlist.size();i++){
                Wfdept dept=dlist.get(i);
                if(i!=0)s.append(",");
                s.append("{"+jsonobj.quote("id")+":"+jsonobj.quote(dept.getId())+","+jsonobj.quote("text")+":"+jsonobj.quote(dept.getName())+","+jsonobj.quote("iconUrl")+":"+jsonobj.quote(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png"));
                List<Wfdept> clist=deptDao.getSubDept(dept.getDeptid().toString());
                if(clist.size()>0)
                    s.append(","+jsonobj.quote("state")+":"+jsonobj.quote("closed"));
                s.append("}");
            }
            s.append("]");
            s.append("}]");
            json=s.toString();
        }
        return jsonobj.JSONDATA+json;
    }
    public String loadDeptTree() throws Exception{
        DeptDao deptDao=new DeptDao();
        JsonObject jsonobj=getJsonObject();
        StringBuffer s=new StringBuffer();
        s.append("[");
        List<Wfdept> dlist=deptDao.getSubDept(deptId);
        for(int i=0;i<dlist.size();i++){
            Wfdept dept=dlist.get(i);
            if(i!=0)s.append(",");
            s.append("{"+jsonobj.quote("id")+":"+jsonobj.quote(dept.getId())+","+jsonobj.quote("text")+":"+jsonobj.quote(dept.getName())+","+jsonobj.quote("iconUrl")+":"+jsonobj.quote(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png"));
            List<Wfdept> clist=deptDao.getSubDept(dept.getDeptid().toString());
            if(clist.size()>0)
                s.append(","+jsonobj.quote("state")+":"+jsonobj.quote("closed"));
            s.append("}");
        }
        s.append("]");
        return jsonobj.JSONDATA+s.toString();
    }
    public String getActions() {
        return actions;
    }


    public void setActions(String actions) {
        this.actions = actions;
    }


    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getParDeptId() {
        return parDeptId;
    }

    public void setParDeptId(String parDeptId) {
        this.parDeptId = parDeptId;
    }

    public Wfdept getBean() {
        return bean;
    }
    public void setBean(Wfdept bean) {
        this.bean = bean;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

    public String[] getDeptIds() {
        return deptIds;
    }

    public void setDeptIds(String[] deptIds) {
        this.deptIds = deptIds;
    }

    public List getSubDeptList() {
        return subDeptList;
    }

    public void setSubDeptList(List subDeptList) {
        this.subDeptList = subDeptList;
    }

}
